package com.sx.pipeline.ui.supply.detail

import android.text.TextUtils
import android.view.View
import androidx.recyclerview.widget.GridLayoutManager
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.sx.mvvmlibs.base.EasyAdapter
import com.sx.pipeline.BR
import com.sx.pipeline.R
import com.sx.pipeline.adapter.ReleaseImageAdapter
import com.sx.pipeline.app.base.CYDataBindingBaseActivity
import com.sx.pipeline.app.config.IntentKey
import com.sx.pipeline.app.config.ReleaseSupplyType
import com.sx.pipeline.arouth.RouteManager
import com.sx.pipeline.arouth.RoutePath
import com.sx.pipeline.databinding.ActSupplyDetailBinding
import com.sx.pipeline.ui.supply.ReleaseSupplyVM


/**
 * @Author: YY
 * @Date: 2022/4/5
 * @Description: 供应商 供应详情
 */
@Route(path = RoutePath.Mine.SUPPLY_DETAIL)
class SupplyDetailActivity :
    CYDataBindingBaseActivity<ActSupplyDetailBinding, ReleaseSupplyVM>(R.layout.act_supply_detail),
    View.OnClickListener {
    @JvmField
    @Autowired(name = IntentKey.ID)
    var supplyId: Int? = 0

    private var mImageAdapter: ReleaseImageAdapter? = null//附件图片适配器

    override fun initData() {
        super.initData()
        mBinding.setVariable(BR.onClick, this)
        mBinding.titleBar.leftView.setOnClickListener {
            finish()
        }
    }

    override fun onResume() {
        super.onResume()
        //请求供应详情
        supplyId?.let { mViewModel.getSupplyOrderDetail(it) }
    }

    override fun initViewObservable() {
        super.initViewObservable()
        observe(mViewModel.supplyDetailEvent){
            when (it?.status) {
                //待审核
                ReleaseSupplyType.PENDING_REVIEW.code -> {
                    mBinding.tvStatus.text=getString(R.string.txt_pending_review)
                    mBinding.tvAction.text=getString(R.string.txt_cancel_release)
                    mBinding.tvAction.visibility=View.VISIBLE
                }
                //待询价
                ReleaseSupplyType.TO_BE_INQUIRED.code -> {
                    mBinding.tvStatus.text=getString(R.string.txt_be_inquired)
                    mBinding.tvAction.text=getString(R.string.txt_cancel_inquire)
                    mBinding.tvAction.visibility=View.VISIBLE
                }
                //已询价
                ReleaseSupplyType.INQUIRED.code -> {
                    mBinding.tvStatus.text=getString(R.string.txt_inquired)
                    mBinding.tvAction.text=getString(R.string.txt_view_inquire)
                    mBinding.tvAction.visibility=View.VISIBLE
                }
                //已取消
                ReleaseSupplyType.CANCELLED.code -> {
                    mBinding.tvStatus.text=getString(R.string.txt_cancelled)
                    mBinding.tvAction.visibility=View.GONE

                }
                //已过期
                ReleaseSupplyType.EXPIRED.code -> {
                    mBinding.tvStatus.text=getString(R.string.txt_expired)
                    mBinding.tvAction.visibility=View.GONE
                }
                //已驳回
                ReleaseSupplyType.REJECTED.code -> {
                    mBinding.tvStatus.text=getString(R.string.txt_rejected)
                    mBinding.tvAction.visibility=View.GONE

                    //显示拒绝原因 和 拒绝时间
                    mBinding.sbRejectReason.rightText = it?.rejectDesc
                    mBinding.sbRejectTime.rightText = it?.auditTime
                    mBinding.sbRejectReason.visibility = View.VISIBLE
                    mBinding.sbRejectTime.visibility = View.VISIBLE
                }
            }
            //单位名称
            mBinding.sbUnitName.rightText=it?.unitName
            //标题
            mBinding.sbTitle.rightText=it?.title
            //供应描述
            mBinding.sbRequirementDetails.rightText=it?.supplyDescribe
            //供应商品信息
            //供应服务区域
            mBinding.sbServiceArea.rightText=it?.addressDetail
            //开始时间
            mBinding.sbStartTime.rightText=it?.startTime
            //结束时间
            mBinding.sbEndTime.rightText=it?.endTime
            //结算方式
            mBinding.sbSettlementMethod.rightText=it?.paymentMethod
            //供应负责人
            mBinding.sbContact.rightText=it?.contact
            //负责人联系电话
            mBinding.sbContactNumber.rightText=it?.contactPhone
            //审核人
            mBinding.sbAuditby.rightText=it?.auditBy
            //供应新建人
            mBinding.sbCreateby.rightText=it?.createBy
            //发布时间
            mBinding.sbCreateTime.rightText=it?.createTime
            //通过时间
            mBinding.sbAuditTime.rightText=it?.auditTime
            if (it?.payMoney?.toInt()!=0){
                //管币价格
                mBinding.tvMoney.text = it?.payMoney.toString()
                mBinding.tabPrice.visibility=View.VISIBLE
            }else{
                mBinding.tabPrice.visibility=View.GONE
            }

            //附件
            if (!TextUtils.isEmpty(it?.annexUrl)){
                it?.annexUrl?.split(",")?.let { it1 -> initDocutmentImg(it1) }
                mBinding.tabImg.visibility=View.VISIBLE
            }
        }

        observe(mViewModel.successSupplyEvent) {
            if (it == true) {
                supplyId?.let { mViewModel.getSupplyOrderDetail(it) }
            }
        }
    }

    //附件
    fun initDocutmentImg(data: List<String>) {
        mImageAdapter = ReleaseImageAdapter(this, data)
        mBinding.rvImg.layoutManager =
            GridLayoutManager(this, 3)
        mBinding.rvImg.adapter = mImageAdapter
        mImageAdapter?.selectMode = EasyAdapter.SelectMode.SINGLE_SELECT
    }


    override fun onClick(v: View?) {
        when(v?.id){
            //查看供应设备详情
            R.id.sb_device_name-> mViewModel.supplyDetailEvent.value?.equipmentId?.let {
                RouteManager.Mine.intentToDeviceDetail(
                    it,true,false
                )
            }
            R.id.tv_action->{
                when (mViewModel.supplyDetailEvent.value?.status) {
                    //待审核
                    ReleaseSupplyType.PENDING_REVIEW.code -> {
                        supplyId?.let { mViewModel.cancelSupplyOrder(it) }
                    }
                    //待询价
                    ReleaseSupplyType.TO_BE_INQUIRED.code -> {
                        supplyId?.let { mViewModel.cancelSupplyOrder(it) }
                    }
                    //已询价
                    ReleaseSupplyType.INQUIRED.code -> {
                        supplyId?.let { RouteManager.Mine.intentTViewInquiry(it) }
                    }
                }

            }
        }
    }

}